<?php

// +----------------------------------------------------------------------
// | 站点配置
// +----------------------------------------------------------------------

namespace Admins\Controller;

use Common\Controller\AdminBase;

class MobileController extends AdminBase {
    
    private $Mobile = null;
    
    protected function _initialize() {
        parent::_initialize();
        $this->Mobile = D('Admins/Mobile');
    }
    
    /**
     * 微信配置
     */
    public function wechat()
    {
        if(IS_POST)
        {
            $post = $_POST;
            if($this->Mobile->saveConfig($post))
            {
                $this->success("更新成功！");
            } 
            else 
            {
                $error = $this->Mobile->getError();
                $this->error($error ? $error : "配置更新失败！");
            }
        } 
        else 
        {
            $configList = $this->Mobile->getConfig();
            $this->assign('Site', $configList);
            $this->display();
        }
    }
    
    /**
     * 搜索列表
     */
    public function searchlist()
    {
        $count = M('WeixinSearch')->count();
        $page = $this->page($count, 20);
        $list = M('WeixinSearch')->order('sort desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign("list", $list);
        $this->assign("Page", $page->show());
        $this->display();
    }
    
    /**
     * 添加热门搜索
     */
    public function addsearch()
    {
        if(IS_POST)
        {
            $post = $_POST;
            if(empty($post['title']))
            {
                $this->error('请输入搜索标题');
            }
            $post['status'] = !empty($post['status']) ? $post['status'] : 0;
            $post['sort'] = !empty($post['sort']) ? $post['sort'] : 0;
            if($this->Mobile->addSearch($post))
            {
                $this->success("信息添加成功！", U('Mobile/searchlist'));
            }
            else 
            {
                $this->error('添加失败');
            }
        }
        else 
        {
            $this->display();
        }
    }
    
    /**
     * 编辑搜索
     */
    public function editsearch()
    {
        if(IS_POST)
        {
            if($this->Mobile->editSearch($_POST)) {
                $this->success("信息更新成功！", U('Mobile/searchlist'));
            } else {
                $error = $this->Mobile->getError();
                $this->error($error ? $error : '信息修改失败！');
            }
        }
        else 
        {
            $id = I('get.id',0,'intval');
            $where = array('id' => $id);
            $info = M('WeixinSearch')->where($where)->find();
            $this->assign('info', $info);
            $this->display();
        }
    }
    
    /**
     * 单个删除
     */
    public function delsearch() {
        $id = I('get.id');
        if (empty($id)) {
            $this->error("没有指定删除对象！");
        }
        //执行删除
        if ($this->Mobile->deleteSearch($id)) {
            $this->success("删除成功！");
        } else {
            $this->error($this->Mobile->getError()? : '删除失败！');
        }
    }
    
    /**
     * 排序
     */
    public function sortsearch()
    {
        foreach ($_POST['sort'] as $id => $sort) {
            M('WeixinSearch')->where(array('id' => $id))->save(array('sort' => $sort));
        }
        $this->success("排序更新成功！");
        exit;
    }
    
    /**
     * 批量处理审核
     */
    public function batchsearch() 
    {
        if (IS_POST) 
        {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            $doAct=I('get.do');
            switch($doAct){
                case 'checkpass':
                    $data=array('status'=>true);
                    $msg='审核';
                    break;
                case 'cancelpass':
                    $data=array('status'=>false);
                    $msg='取消审核';
                    break;
            }
            foreach ($ids as $id) {
                M('WeixinSearch')->where(array('id'=>$id))->save($data);
            }
            $this->success($msg.'成功！');
        }
    }
    
    /**
     * 批量删除搜索
     */
    public function deleteallsearch(){
        if (IS_POST) {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            foreach ($ids as $id) {
                if($this->Mobile->deleteSearch($id)){
                    continue;
                }else{
                    $error = $this->Mobile->getError();
                    $this->error($error ? $error : '删除信息失败！');
                }
            }
            $this->success('信息删除成功！');
        }
    }
    
    /**
     * 广告列表
     */
    public function adlist()
    {
        $count = M('WeixinAd')->count();
        $page = $this->page($count, 20);
        $list = M('WeixinAd')->limit($page->firstRow . ',' . $page->listRows)->order(array('id' => 'DESC'))->select();
        if($list)
        {
            foreach($list as $k => $v)
            {
                $list[$k]['position'] = '首页轮播';
                if($v['position'] == 2)
                {
                    $list[$k]['position'] = '首页导师入口';
                }
                elseif($v['position'] == 3)
                {
                    $list[$k]['position'] = '导师轮播';
                }
            }
        }
        $this->assign("list", $list);
        $this->assign("Page", $page->show());
        $this->display();
    }
    
    /**
     * 添加广告
     */
    public function addad()
    {
        if(IS_POST)
        {
            $post=$_POST;
            $post['create_time']=time();
            if ($this->Mobile->addAd($post)) {
                $this->success("信息添加成功！", U('Mobile/adlist'));
            } else {
                $error = $this->Mobile->getError();
                $this->error($error ? $error : '信息添加失败！');
            }
        }
        else
        {
            $this->display();
        }
    }
    
    /**
     * 修改广告
     */
    public function editad()
    {
        if(IS_POST){
            if ($this->Mobile->editAd($_POST)) {
                $this->success("信息更新成功！", U('Mobile/adlist'));
            } else {
                $error = $this->Mobile->getError();
                $this->error($error ? $error : '信息修改失败！');
            }
        }else{
            $id = I('get.id',0,'intval');
            $where = array('id'=>$id);
            $info = M('WeixinAd')->where($where)->find();
            $this->assign('info', $info);
            $this->display();
        }
    }
    
    /**
     * 删除单个广告
     */
    public function delad() {
        $id = I('get.id');
        if (empty($id)) {
            $this->error("没有指定删除对象！");
        }
        //执行删除
        if ($this->Mobile->deleteAd($id)) {
            $this->success("删除成功！");
        } else {
            $this->error(D('Admins/Advert')->getError()? : '删除失败！');
        }
    }
    
    /**
     * 广告排序
     */
    public function sortad()
    {
        foreach ($_POST['sort'] as $id => $sort) {
            M('WeixinAd')->where(array('id' => $id))->save(array('sort' => $sort));
        }
        $this->success("排序更新成功！");
        exit;
    }
    
    /**
     * 批量处理审核
     */
    public function batchad()
    {
        if (IS_POST)
        {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            $doAct=I('get.do');
            switch($doAct){
                case 'checkpass':
                    $data=array('status'=>true);
                    $msg='审核';
                    break;
                case 'cancelpass':
                    $data=array('status'=>false);
                    $msg='取消审核';
                    break;
            }
            foreach ($ids as $id) {
                M('WeixinAd')->where(array('id'=>$id))->save($data);
            }
            $this->success($msg.'成功！');
        }
    }
    
    //批量删除信息及信息附件
    public function deleteallad(){
        if (IS_POST) {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            foreach ($ids as $id) {
                if($this->Mobile->deleteAd($id)){
                    continue;
                }else{
                    $error = $this->Mobile->getError();
                    $this->error($error ? $error : '删除信息失败！');
                }
            }
            $this->success('信息删除成功！');
        }
    }
    
    /**
     * 导航列表
     */
    public function navlist()
    {
        $count = M('WeixinNav')->count();
        $page = $this->page($count, 20);
        $list = M('WeixinNav')->limit($page->firstRow . ',' . $page->listRows)->order(array('sort' => 'DESC'))->select();
        $this->assign("list", $list);
        $this->assign("Page", $page->show());
        $this->display();
    }
    
    /**
     * 添加导航
     */
    public function addnav()
    {
        if(IS_POST)
        {
            $post=$_POST;
            $post['create_time']=time();
            if ($this->Mobile->addNav($post)) {
                $this->success("信息添加成功！", U('Mobile/navlist'));
            } else {
                $error = $this->Mobile->getError();
                $this->error($error ? $error : '信息添加失败！');
            }
        }
        else 
        {
            $this->display();
        }
    }
    
    /**
     * 修改导航
     */
    public function editnav()
    {
        if(IS_POST){
            if ($this->Mobile->editNav($_POST)) {
                $this->success("信息更新成功！", U('Mobile/navlist'));
            } else {
                $error = $this->Mobile->getError();
                $this->error($error ? $error : '信息修改失败！');
            }
        }else{
            $id = I('get.id',0,'intval');
            $where = array('id'=>$id);
            $info = M('WeixinNav')->where($where)->find();
            $this->assign('info', $info);
            $this->display();
        }
    }
    
    /**
     * 删除单个导航
     */
    public function delnav() {
        $id = I('get.id');
        if (empty($id)) {
            $this->error("没有指定删除对象！");
        }
        //执行删除
        if ($this->Mobile->deleteNav($id)) {
            $this->success("删除成功！");
        } else {
            $this->error(D('Admins/Advert')->getError()? : '删除失败！');
        }
    }
    
    /**
     * 广告排序
     */
    public function sortnav()
    {
        foreach ($_POST['sort'] as $id => $sort) {
            M('WeixinNav')->where(array('id' => $id))->save(array('sort' => $sort));
        }
        $this->success("排序更新成功！");
        exit;
    }
    
    /**
     * 批量处理审核
     */
    public function batchnav()
    {
        if (IS_POST)
        {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            $doAct=I('get.do');
            switch($doAct){
                case 'checkpass':
                    $data=array('status'=>true);
                    $msg='审核';
                    break;
                case 'cancelpass':
                    $data=array('status'=>false);
                    $msg='取消审核';
                    break;
            }
            foreach ($ids as $id) {
                M('WeixinNav')->where(array('id'=>$id))->save($data);
            }
            $this->success($msg.'成功！');
        }
    }
    
    /**
     * 批量删除导航
     */
    public function deleteallnav(){
        if (IS_POST) {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            foreach ($ids as $id) {
                if($this->Mobile->deleteNav($id)){
                    continue;
                }else{
                    $error = $this->Mobile->getError();
                    $this->error($error ? $error : '删除信息失败！');
                }
            }
            $this->success('信息删除成功！');
        }
    }
    
    /**
     * 活动设置
     */
    public function activity()
    {
        if(IS_POST)
        {
            if($this->Mobile->saveActivity($_POST)){
                $this->success('修改成功');
            }else{
                $error = $this->Mobile->getError();
                $this->error($error ? $error : '修改失败！');
            }
        }
        else
        {
            $info = $this->Mobile->getActivity();
            $this->assign('info', $info);
            $this->display();
        }
    }
    
    /**
     * 类别管理
     */
    public function category()
    {
        if(IS_POST)
        {
            if($this->Mobile->saveCategoy($_POST))
            {
                $this->success('修改成功');
            }
            else
            {
                $error = $this->Mobile->getError();
                $this->error($error ? $error : '修改失败！');
            }
        }
        else 
        {
            $info = $this->Mobile->getCategory();
            $this->assign('info', $info);
            $this->display();
        }
    }
    
    /**
     * 导师类别管理
     */
    public function teach()
    {
        if(IS_POST)
        {
            if($this->Mobile->saveTeach($_POST))
            {
                $this->success('修改成功');
            }
            else
            {
                $error = $this->Mobile->getError();
                $this->error($error ? $error : '修改失败！');
            }
        }
        else 
        {
            $info = M('Teachcate')->where(array('status' => 1))->order('sort desc')->select();
            if($info)
            {
                foreach($info as $k => $v)
                {
                    $info[$k]['image'] = M('WeixinTeachcate')->where(array('cateid' => $v['catid']))->getField('image');
                }
            }
            $this->assign('info', $info);
            $this->display();
        }
    }
    
}